package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.vo.UserRequestVo;
import com.gxa.common.vo.UserVo;
import com.gxa.entity.SysUser;
import com.gxa.mapper.AttendenceMapper;
import com.gxa.mapper.SysUserMapper;
import com.gxa.service.SysUserService;
import com.gxa.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    AttendenceMapper attendenceMapper;


    @Override
    public R findUserByDepId(Integer depId) {
        R r=new R();
        if (depId!=null){
            try {
                List<SysUser> sysUserList = sysUserMapper.findUserByDepId(depId);
                r.setCode(200);
                r.setMsg("success");
                r.setData(sysUserList);

            }catch (Exception e){
                e.printStackTrace();
                r.setCode(500);
                r.setMsg("服务器开小差了，请联系管理员");
            }
        }else {
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,timeout=5,rollbackFor = {Exception.class})
    public R delUserById(Integer id) {
        R r=new R();
        if (id!=null){
            try {
                sysUserMapper.delUserById(id);
                attendenceMapper.delAttendanceByUserId(id);
                r.setCode(200);
                r.setMsg("success");
            }catch (Exception e){
                throw new RuntimeException("数据库发生了异常！！！"+e.getMessage());
            }
        }else {
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }

    /**
     * 分页查询所有用户列表
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @return R
     */
    @Override
    public R selectAllUser(Integer currentPage, Integer pageSize) {
        R r=new R();
        if (currentPage!=null&&pageSize!=null){
            try{
                PageHelper.startPage(currentPage,pageSize);
                List<UserVo> sysUsers = sysUserMapper.selectAllUser();
                PageInfo<SysUser> pageInfo = new PageInfo(sysUsers);
                r.setCode(200);
                r.setMsg("success");
                r.setData(pageInfo);

            }catch (Exception e){
                throw new RuntimeException("发生了异常"+e.getMessage());
            }
        }else{
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }

    /**
     * 根据真实姓名查询用户
     * @param condition 用户真实名字
     * @param currentPage currentPage 当前页
     * @param pageSize 每页大小
     * @return
     */
    @Override
    public R selectUserByConditions(String condition, Integer currentPage, Integer pageSize) {
        R r=new R();
        if (condition!=null&&currentPage!=null&&pageSize!=null){
            try{
                PageHelper.startPage(currentPage,pageSize);
                List<UserVo> sysUsers = sysUserMapper.selectUserByName(condition);
                PageInfo<SysUser> pageInfo = new PageInfo(sysUsers);
                r.setCode(200);
                r.setMsg("success");
                r.setData(pageInfo);
            }catch (Exception e){
                throw new RuntimeException("发生了异常"+e.getMessage());
            }
        }else {
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }


    /**
     * 根据条件查询用户
     * @param userRequestVo 请求查询的实体
     * @return
     */
    @Override
    public R selectUserByCondition(UserRequestVo userRequestVo) {
        R r=new R();
        if (userRequestVo!=null){
            try{
                Integer currentPage = userRequestVo.getCurrentPage();
                Integer pageSize = userRequestVo.getPageSize();
                PageHelper.startPage(currentPage,pageSize);
                List<UserVo> sysUsers = sysUserMapper.selectUserByCondition(userRequestVo);
                PageInfo<UserVo> pageInfo = new PageInfo(sysUsers);
                r.setCode(200);
                r.setMsg("success");
                r.setData(pageInfo);

            }catch (Exception e){
                throw new RuntimeException("发生了异常"+e.getMessage());
            }
        }else {
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }


    /**
     * 添加用户实体
     * @param userVo 用户实体
     * @return
     */
    @Override
    public R addUser(UserVo userVo) {
        R r=new R();
        if(userVo!=null){
            try{
                sysUserMapper.addUser(userVo);
                r.setCode(200);
                r.setMsg("success");
                return r;
            }catch (Exception e){
                throw new RuntimeException("发生了异常"+e.getMessage());
            }
        }else {
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }

    /**
     * 根据用户id回显用户信息
     * @param id 用户id
     * @return R
     */
    @Override
    public R selectUserById(Integer id) {
        R r=new R();
        if(id!=null){
            try{
                UserVo userVo = sysUserMapper.selectUserById(id);
                r.setCode(200);
                r.setMsg("success");
                r.setData(userVo);
                return r;
            }catch (Exception e){
                throw new RuntimeException("发生了异常"+e.getMessage());
            }
        }else {
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }

    /**
     * 修改用户实体
     * @param userVo 用户实体
     * @return R
     */
    @Override
    public R updateUserById(UserVo userVo) {
        R r=new R();
        if(userVo!=null){
            try{
                Byte isUpdatePwd = userVo.getIsUpdatePwd();
                if (isUpdatePwd!=null&&isUpdatePwd==0){
                    sysUserMapper.updatePwd("8f7faab507a7db84f231414c59e7a3e3",userVo.getId());
                }
                sysUserMapper.updateUserById(userVo);
                r.setCode(200);
                r.setMsg("success");
                return r;
            }catch (Exception e){
                throw new RuntimeException("发生了异常"+e.getMessage());
            }
        }else {
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }

    /**
     * 根据用户id添加用户头像
     * @param img 图片
     * @param id 用户id
     * @param request
     * @return
     */
    @Override
    public R addUserImg(MultipartFile img, Integer id, HttpServletRequest request) {
        R r = new R<>();
        try{
            String imgUrl = FileUtils.saveImage(img, request, 1);
            sysUserMapper.updateUserImg(imgUrl,id);
            r.setCode(200);
            r.setMsg("添加图片成功");
            return r;
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
            return r;
        }
    }
}
